package com.bto.solar.work.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson2.JSONObject;
import com.bto.solar.work.common.R;
import com.bto.solar.work.constant.BizStageEnum;
import com.bto.solar.work.constant.BizTaskEnum;
import com.bto.solar.work.dao.IFddTaskService;
import com.bto.solar.work.entity.FddTask;
import com.bto.solar.work.fdd.BtoFddNotifyService;
import com.bto.solar.work.fdd.FddService;
import com.bto.solar.work.fdd.client.FddSignTaskClient;
import com.bto.solar.work.utils.NullUtils;
import com.fasc.open.api.v5_1.res.signtask.SignTaskActorGetUrlRes;
import com.fasc.open.api.v5_1.res.signtask.SignTaskDetailRes;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author adrian
 * @since 2023-08-09
 */
@Slf4j
@RestController
@RequestMapping("/fdd")
public class FddTaskController {
    @Autowired
    private FddService fddService;
    @Autowired
    private IFddTaskService fddTasksService;
    @Autowired
    private FddSignTaskClient fddSignTaskClient;
    @Autowired
    private BtoFddNotifyService btoFddNotifyService;

    @ApiModelProperty("获取参与方专属链接")
    @GetMapping("get-actor-url")
    public R getActorUrl(@RequestParam String orderId , @RequestParam String signeType)  {
        SignTaskActorGetUrlRes actorUrl = fddService.getActorUrl(orderId, signeType, BizStageEnum.PRE_APPROVAL.getStageId(),
                BizTaskEnum.TASK_YSXX.getTaskId());
        return R.ok(actorUrl);
    }

    @ApiModelProperty("查询信息使用签署")
    @GetMapping("get-preview-url")
    public R getPreviewUrl(@RequestParam String orderId)  {
        return R.ok(fddService.getOwnerDownloadUrl(orderId));
    }

    @ApiModelProperty("作废签约")
    @GetMapping("abolish")
    public R signedAbolish(@RequestParam String orderId , @RequestParam(required = false) String reason)  {
        String abolishSignTaskId = fddService.abolishSignTask(orderId, reason);
        SignTaskDetailRes appDetail = fddSignTaskClient.getAppDetail(abolishSignTaskId);
        JSONObject res = new JSONObject();
        res.put("actorSignTaskUrl" , appDetail.getActors().get(0).getActorSignTaskUrl());
        res.put("actorSignTaskEmbedUrl" , appDetail.getActors().get(0).getActorSignTaskEmbedUrl());
        return R.ok(res);
    }

    @ApiModelProperty("法大大回调")
    @PostMapping("notify")
    public String fddNotify(@RequestHeader HttpHeaders headers,
                            @RequestParam("bizContent") String bizContent)  {
        //生产环境下，同步到城北旧系统
        if("prod".equals(SpringUtil.getActiveProfile())){
            JSONObject signTaskEvent = JSONObject.parseObject(bizContent);
            String signTaskId = signTaskEvent.getString("signTaskId");
            FddTask fddTask = fddTasksService.lambdaQuery().eq(FddTask::getSignTaskId, signTaskId).one();
            if (NullUtils.isNull(fddTask)) {
                log.info("fdd_tasks  表无法找到，法大大回调，签署完成 的 signTaskId {} , 转发给城北系统", signTaskId);
                return btoFddNotifyService.fddNotifyChengBeiSystem(headers , bizContent);
            }
        }

        boolean res = btoFddNotifyService.fddNotify(headers, bizContent);
        if(res){
            return "{\"msg\":\"success\"}\n";
        }
        else {
            return "{\"msg\":\"failed\"}\n";
        }
    }


}
